--
-- Author: shunguo.chen
-- Date: 2016-06-17 11:42:45
--
-- ControllerBase
--

local fileName = string.getModeName(...)
local ControllerBase = class(fileName)

ControllerBase._eventHandles = nil
ControllerBase._parentType = nil

function ControllerBase:ctor(param)
	param = param or {}
    self._parentType = param.type
    self._zOrder = param.zOrder or 0

    -- 默认处理的那个view
    self._view = nil

    self:_addListeners()
    self:init()
end

function ControllerBase:getParentType()
	return self._parentType
end
function ControllerBase:setParentType(type)
	self._parentType = type
end

function ControllerBase:addChild(node, param)
	if self._parentType == "world" then
		param = tonumber(param) or 0
		app:getLaunchCtl():addChild(node, self._zOrder + param)
	elseif self._parentType == "systemUI" then
		if type(param) ~= "table" then
			param = {}
		end
		UIHandler:addSystemUi(node, node.__cname, param.notDelete, param.isFade)
	elseif self._parentType == "popupUI" then
		if type(param) ~= "table" then
			param = {}
		end
		UIHandler:addPopupUi(node, node.__cname, param.notDelete)
	elseif self._parentType == "storyUI" then
		if type(param) ~= "table" then
			param = {}
		end
		UIHandler:addStoryUi(node, node.__cname, param.notDelete)
	end
end

function ControllerBase:removeChild(node, param)
	if self._parentType == "world" then
		app:getLaunchCtl():removeChild(node)
	elseif self._parentType == "systemUI" then
		if type(param) ~= "table" then
			param = {}
		end
		UIHandler:removeSystemUiByKey(node.__cname, param.isFade)
	elseif self._parentType == "popupUI" then
		if type(param) ~= "table" then
			param = {}
		end
		UIHandler:removePopupUiByKey(node.__cname)
	elseif self._parentType == "storyUI" then
		if type(param) ~= "table" then
			param = {}
		end
		UIHandler:removeStoryUiByKey(node.__cname)
	end
end

function ControllerBase:_addListeners()
	self._eventHandles = {}
	local eventNames = self:listEvent()
	for _, eventName in ipairs(eventNames) do
		local handle = EM:on(eventName, handler(self, self.onEvent))
		table.insert(self._eventHandles, handle)
	end
end

function ControllerBase:addListeners(eventNames, listener)
	for _, eventName in ipairs(eventNames) do
		local handle = EM:on(eventName, listener)
		table.insert(self._eventHandles, handle)
	end
end

function ControllerBase:removeListeners()
	for _, handle in ipairs(self._eventHandles) do
		EM:remove(handle)
	end
	self._eventHandles = nil
end

function ControllerBase:init()

end

function ControllerBase:closeView(data)
	if self._view then
		self:removeChild(self._view, data)
	end
end

function ControllerBase:clearView()
	if self._view then
		self._view = nil
	end
end

function ControllerBase:hasView()
	return self._view ~= nil
end

function ControllerBase:isViewVisible()
	return not cc.isDead(self._view) and self._view:isVisible()
end

function ControllerBase:isViewAlive()
	return not cc.isDead(self._view)
end
---------------- event -----------------------
function ControllerBase:onEvent(eventName, data)

end

function ControllerBase:listEvent()
    return {}
end

function ControllerBase:notify(...)
	EM:notify(...)
end

function ControllerBase:getControllerName()
	return self.__cname
end

return ControllerBase
